/**
 * @param {string} s1
 * @param {string} s2
 * @param {string} s3
 * @return {boolean}
 */
export var isInterleave = function (s1, s2, s3) {
  if (s1.length + s2.length !== s3.length) return false
  // dp s1的前i个字符和s2的前j个字符是否可以组成s3的前i+j个字符
  const dp = new Array(s1.length + 1).fill(0).map(() => new Array(s2.length + 1).fill(0))
  dp[0][0] = true
  for (let i = 1; i < s1.length + 1; i++) {
    dp[i][0] = dp[i - 1][0] && s1[i - 1] == s3[i - 1]
  }
  for (let j = 1; j < s2.length + 1; j++) {
    dp[0][j] = dp[0][j - 1] && s2[j - 1] == s3[j - 1]
  }
  for (let i = 1; i <= s1.length; i++) {
    for (let j = 1; j <= s2.length; j++) {
      dp[i][j] =
        (dp[i - 1][j] && s1[i - 1] == s3[i + j - 1]) || (dp[i][j - 1] && s2[j - 1] == s3[i + j - 1])
    }
  }
  return dp[s1.length][s2.length]
}
